/*
 *     Prionace - A file navigator to manage your files
 *
 *     Copyright (C) 2012 Francivan Bezerra
 *
 *     This file is part of Prionace.
 *
 *     Prionace is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Prionace is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Prionace.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.fba.prionace.file;

import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.io.File;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.TransferHandler;

import org.fba.prionace.ui.ExceptionHandler;
import org.fba.prionace.ui.viewer.Viewer;

public class FileTransferHandler extends TransferHandler {

	public boolean canImport(JComponent comp, DataFlavor[] transferFlavors) {
		if (comp instanceof JTabbedPane) {
			JTabbedPane tab = (JTabbedPane) comp;
			int tabIndex = tab.getUI().tabForCoordinate(tab,
					tab.getMousePosition().x, tab.getMousePosition().y);

			if (tabIndex != -1) {
				tab.setSelectedIndex(tabIndex);
			}
			return false;
		}
		return hasJavaFileListTypeFlavor(transferFlavors);
	}

	protected Transferable createTransferable(JComponent c) {
		Viewer viewer = (Viewer) c;
		FileTransferable transferable = null;

		if (viewer.getSelectedFiles().size() > 0) {
			transferable = new FileTransferable(viewer.getSelectedFiles());
		}

		return transferable;
	}

	protected void exportDone(JComponent source, Transferable data, int action) {
		Viewer viewer = (Viewer) source;
		if (action == MOVE) {
			viewer.refreshFilesList();
		}
	}

	public int getSourceActions(JComponent c) {
		return COPY_OR_MOVE;
	}

	public boolean importData(TransferSupport support) {
		if (!canImport((JComponent) support.getComponent(),
				support.getDataFlavors())) {
			return false;
		}

		Viewer viewer = (Viewer) support.getComponent();
		JPanel pane = (JPanel) viewer.getComponent(0).getComponentAt(0, 0);
		Point dropPoint = support.getDropLocation().getDropPoint();
		JComponent dropComponent = (JComponent) pane.getComponentAt(dropPoint);
		File dropDir;

		if (dropComponent instanceof JFile) {
			dropDir = ((JFile) dropComponent).getFile();
			if (dropDir.isFile()) {
				dropDir = dropDir.getParentFile();
			}
		} else {
			dropDir = viewer.getCurrentPath();
		}

		try {
			@SuppressWarnings("unchecked")
			List<File> files = (List<File>) support.getTransferable()
					.getTransferData(DataFlavor.javaFileListFlavor);
			FileCopyOrMove copyOrMove = new FileCopyOrMove(files, dropDir,
					viewer, support.getDropAction() == TransferHandler.MOVE);

			if (copyOrMove.showConfirmOperationDialog()) {
				copyOrMove.executeOperation();
			}
		} catch (Exception e) {
			ExceptionHandler.showException(e, viewer);
		}
		return true;
	}

	private boolean hasJavaFileListTypeFlavor(DataFlavor[] transferFlavors) {
		for (DataFlavor flavor : transferFlavors) {
			if (flavor.isFlavorJavaFileListType()) {
				return true;
			}
		}
		return false;
	}
}
